package solutions.ferpa.ztest.intf;

import br.com.telescope.adsrt.dom.DomLogin;
import br.com.telescope.adsrt.dom.DomPasswordHash;
import br.com.telescope.adsrt.lib.*;
import br.com.telescope.adsrt.lib.Field;
import br.com.telescope.t2.AccessPolicy;
import br.com.telescope.t2.ServiceModel;
import br.com.telescope.t2.exception.ApplicationRuntimeException;
import br.com.telescope.t2.exception.Severity;
import br.com.telescope.t2.feature.CustomFeature;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import solutions.ferpa.ztest.ent.*;

/**
 * Criador automático de tokens.
 * <p>
 * Permite a criação de tokens de autenticação de forma remota.
 * <P>
 * Telescope origin: RTAUTH_UTIL.UTL_CREATE_TOKEN
 * <P>
 * @author TELESCOPE - Build task T2 Web Interface Generator
 * <P>
 * @version 0.1
 */
public class UtlCreateToken extends ServiceModel {
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    public static final String GLOBAL_NAME = "RTAUTH_UTIL.UTL_CREATE_TOKEN";
    
    @Override
    public String globalName() {
        return GLOBAL_NAME;
    }
    
    {
        setTitle("Criador automático de tokens");
    }
    
    
    // Service parameters
    private DomLogin pLogin;
    private DomPasswordHash pPassword;
    
    // Record items
    
    // Query items
    
    @Override
    public void initAttributes() {
        
    }
    
    @Override
    public void initParameters() {
        pLogin = new DomLogin(ctx, "LOGIN", "Login");
        pLogin.setValue(ctx.getParameter("P_LOGIN"));
        pLogin.setUnbound(true);
        pPassword = new DomPasswordHash(ctx, "PASSWORD", "Password");
        pPassword.setValue(ctx.getParameter("P_PASSWORD"));
        pPassword.setUnbound(true);
    }
    
    @Override
    public List<Field> getParameters() {
        if (allParameters == null) {
            allParameters = new ArrayList<Field>();
            allParameters.add(pLogin);
            allParameters.add(pPassword);
        }
        return allParameters;
    }
    
    @Override
    public String getDefaultOrderBy() {
        return "";
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="CUSTOM">
    public class FCustom extends CustomFeature {
        
        @Override
        public void initForm() {
        }
        
        @Override
        public int getAccessPolicy() {
            return AccessPolicy.PUBLIC;
        }
        
        @Override
        public void execute() {
            try {
                custom();
            } catch (AdsException ex) {
                throw new ApplicationRuntimeException(ex);
            }
        }
        
        {
            setFeatureName("CUSTOM");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="initFeatures">
    
    @Override
    protected void initFeatures() {
        addFeature(new FCustom());
        super.initFeatures();
    }
    
    @Override
    public String getDefaultFeature() {
        return "CUSTOM";
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="Getters and Setters">
    
    public String getParameterLogin() {
        return pLogin.getValue();
    }
    
    public void setParameterLogin(String value) {
        pLogin.setValue(value);
    }
    
    public String getParameterPassword() {
        return pPassword.getValue();
    }
    
    public void setParameterPassword(String value) {
        pPassword.setValue(value);
    }
    // </editor-fold>
    
    /**
     * null.
     */
    public void custom() throws AdsException {
        // <user-code id="6262518" parentId="6262516" hashcode="-b8b474d" length="689">
        // import br.com.telescope.t2.exception.Severity;
        // import java.util.UUID;
        setShown(true);
        ctx.setContentType("text/plain");
        try {
            EntUsuarios usuario = new EntUsuarios(ctx);
            usuario.findByLogin(pLogin.getValue());
            if (!usuario.getSenhaHash().equals(pPassword) || !usuario.valueOfAtivo().equals("S")) {
                throw new AdsException("", Severity.ERROR, "Usuário ou senha inválida!");
            }
            String token = UUID.randomUUID().toString().toUpperCase();
            ctx.setPreference("RT.TOKEN." + token, "USER_ID=" + usuario.valueOfId());
            ctx.output("OK");
            ctx.output(token);
        } catch (Throwable ex) {
            ctx.output("ERR");
            ctx.output(ex.getMessage());
        }
        // </user-code>
    }
}
